package com.ws;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;

@Mojo(name = "countJava",defaultPhase= LifecyclePhase.PACKAGE)
public class MyMojo extends AbstractMojo {
	private static String[] file_include={"java","xml","properties"};
	@Parameter
	private File basedir;
	@Parameter
	private File sourceDirectory;
	@Parameter
	private File testSourceDirectory;
	
	
	@Parameter
	private String[] files;
	
	
	public void execute() throws MojoExecutionException {
		if(files == null || files.length == 0){
			files = file_include;
		}
		getCount(sourceDirectory);
		getCount(testSourceDirectory);
	}
	
	public void getCount(File sourceFile){
		List<File> listFile = new ArrayList<File>(); 
		countList(listFile, sourceFile);
		 String path = sourceFile.getAbsolutePath().substring(basedir.getAbsolutePath().length());   
	   getLog().info(path + ": " + 2 + " lines of code in " + listFile.size() + " files");   
		
	}
	
	public void countList(List<File> listFile,File sourceFile){
		if(sourceFile.isFile()){
			for(String include : files){
				if(sourceFile.getName().endsWith("."+include)){
					listFile.add(sourceFile);
					break;
				}
			}
		}else{
			for(File fileChild : sourceFile.listFiles()){
				countList(listFile, fileChild);
			}
		}
	}
}
